package com.ssy.lingxi.marketing.serviceimpl;

import cn.hutool.core.collection.CollectionUtil;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.marketing.model.dto.ActivityInnerRecordRespDTO;
import com.ssy.lingxi.marketing.model.dto.ActivityOuterRecordRespDTO;
import com.ssy.lingxi.marketing.model.dto.ActivityRecordPageReqDTO;
import com.ssy.lingxi.marketing.model.vo.common.request.OperateLogRequest;
import com.ssy.lingxi.marketing.model.vo.common.response.OperateLogInnerResponse;
import com.ssy.lingxi.marketing.model.vo.common.response.OperateLogOuterResponse;
import com.ssy.lingxi.marketing.service.IOperateLogService;
import com.ssy.lingxi.marketing.serviceimpl.base.activity.BaseActivityRecordService;
import com.ssy.lingxi.marketing.serviceimpl.base.coupon.BaseCouponRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  平台后台-系统管理-操作日志
 * @author ds
 * @version 2.0.0
 * @date 2021/06/17
 */
@Slf4j
@Service
public class OperateLogServiceImpl implements IOperateLogService {

    @Resource
    private BaseCouponRecordService baseCouponRecordService;
    @Resource
    private BaseActivityRecordService baseActivityRecordService;

    private ActivityRecordPageReqDTO buildActivityRecordPageReqDTO(OperateLogRequest req){
        ActivityRecordPageReqDTO dto=new ActivityRecordPageReqDTO();
        dto.setDataId(req.getId());
        dto.setStartTime(req.getStartTime());
        dto.setEndTime(req.getEndTime());
        dto.setPageSize(req.getPageSize());
        dto.setCurrent(req.getCurrent());
        dto.setSort(Boolean.FALSE);
        return dto;
    }


    @Override
    public Wrapper<PageData<OperateLogOuterResponse>> pageOuterLog(OperateLogRequest request) {
        PageData<ActivityOuterRecordRespDTO> outerRecordPageData;
        switch (request.getOperateType()){
            case 1 :
                //平台活动外部记录
                outerRecordPageData = baseActivityRecordService.pagePlatformActivityOuterRecordCommon(null, buildActivityRecordPageReqDTO(request));
                break;
            case 2 :
                //商家活动外部记录
                outerRecordPageData=baseActivityRecordService.pageMerchantActivityOuterRecordCommon(null,buildActivityRecordPageReqDTO(request));
                break;
            default:
                outerRecordPageData=new PageData<>(0L,null);
        }
        List<OperateLogOuterResponse> responseList=null;
        if(CollectionUtil.isNotEmpty(outerRecordPageData.getData())){
            responseList = outerRecordPageData.getData().stream().map(o -> {
                OperateLogOuterResponse response = new OperateLogOuterResponse();
                BeanUtils.copyProperties(o, response);
                response.setBelongType(request.getOperateType());
                response.setId(o.getDataId());
                return response;
            }).collect(Collectors.toList());
        }
        return Wrapper.success(new PageData<>(outerRecordPageData.getTotalCount(),responseList));
    }



    @Override
    public Wrapper<PageData<OperateLogInnerResponse>> pageInnerLog(OperateLogRequest request) {
        PageData<ActivityInnerRecordRespDTO> innerRecordPageData;
        switch (request.getOperateType()){
            case 1 :
                //平台活动内部记录
                innerRecordPageData = baseActivityRecordService.pagePlatformActivityInnerRecordCommon(null, buildActivityRecordPageReqDTO(request));
                break;
            case 2 :
                //商户活动内部记录
                innerRecordPageData=baseActivityRecordService.pageMerchantActivityInnerRecordCommon(null,buildActivityRecordPageReqDTO(request));
                break;
            case 3 :
                //平台优惠券内部记录
                innerRecordPageData = baseCouponRecordService.pagePlatformActivityInnerRecordCommon(null, buildActivityRecordPageReqDTO(request));
                break;
            case 4 :
                //商户优惠券内部记录
                innerRecordPageData=baseCouponRecordService.pageMerchantActivityInnerRecordCommon(null,buildActivityRecordPageReqDTO(request));
                break;
            default:
                innerRecordPageData=new PageData<>(0L,null);
        }
        List<OperateLogInnerResponse> responseList=null;
        if(CollectionUtil.isNotEmpty(innerRecordPageData.getData())){
            responseList = innerRecordPageData.getData().stream().map(o -> {
                OperateLogInnerResponse response = new OperateLogInnerResponse();
                BeanUtils.copyProperties(o, response);
                response.setId(o.getDataId());
                Integer operateType = request.getOperateType();
                response.setBelongType(operateType>2?operateType-2:operateType);
                return response;
            }).collect(Collectors.toList());
        }
        return Wrapper.success(new PageData<>(innerRecordPageData.getTotalCount(),responseList));
    }
}
